<?php

class AdminRightsModel extends \think\Model
{
    protected $table = 'admin_rights';
    protected $pk = 'id';

    public function parent()
    {
        return $this->hasOne(self::class, 'parent_id');
    }

    /**
     * 获得所有controller,action
     * @param string $module
     * @return array
     */
    static function getControllers($module = 'Admin')
    {
        $controllerDir = APPLICATION_PATH . "/application/modules/{$module}/controllers";
        if (!is_dir($controllerDir)) return array();
        $controllerList = scandir($controllerDir);
        $controller = array();
        foreach ($controllerList as $file) {
            if (!in_array($file, array(
                '.',
                '..',
                //'Ajax.php',
                'Login.php'
            ))
            ) {
                $content = file_get_contents($controllerDir . '/' . $file);
                if (strpos($content, 'AdminController')) {
                    preg_match_all("/[\S]*Action\(/", $content, $methods);
                    foreach ($methods as $item) {
                        foreach ($item as $action) {
                            $controller[strtolower(trim($file, '.php'))][] = strtolower(preg_replace('/Action\(/', '', $action));
                        }
                    }
                }
            }
        }
        return $controller;
    }

    /**
     * 获取所有模块列表
     * @return array
     */
    static function getModule()
    {
        $modules = explode(',', Yaf_Registry::get('config')->application->modules);
        return $modules;
    }

    static function get_children_right($parentid = 0, $count = -4, $ismerge = true)
    {
        $result = array();
        $count = $count + 4;
        $data = AdminRightsModel::where('parent_id', $parentid)->order('id desc')->select();
        if ($data) {
            foreach ($data as $item) {
                $parent = AdminRightsModel::where('id', $item['parent_id'])->find();
                $children = self::get_children_right($item['id'], $count, $ismerge);
                $result[] = array(
                    'id' => $item['id'],
                    'parent_id' => $item['parent_id'],
                    'name' => $ismerge ? str_repeat('&nbsp;', $count) . Helper::removeXSS($item['name']) : Helper::removeXSS($item['name']),
                    'parent_name' => $parent ? $parent['name'] : '-',
                    'children' => $children
                );
                if ($ismerge && $children) {
                    foreach ($children as $item2) {
                        $result[] = $item2;
                    }
                }
            }
        }
        return $result;
    }

    static function get_right_title($uri, $default_title = '')
    {
        $uri = str_replace('/', '@', trim($uri,'/'));
        $allright = self::select();
        foreach ($allright as $right) {
            $right_content = explode(',', strtolower($right->content));
            if ($right->content != strtolower($right->content)) {
                $right->content = strtolower($right->content);
                $right->save();
            }
            if ($right_content && in_array(strtolower($uri), $right_content)) return $right->name;
        }
        return $default_title;
    }

    // 单用户所有权限
    static function get_user_rights()
    {
        $user = Yaf_Session::getInstance()->get(AdminController::admin_auth_session_key);
        if (!$user) return '';
        $user = unserialize($user);
        $userRight = ",,admin@index@index,{$user['rights']},";
        return $userRight;
    }
}
